package com.baor.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baor.dao.InvoiceDao;
import com.baor.domain.FinanceVo;
import com.baor.domain.InvoiceVo;
import com.baor.domain.field.InvoiceTypeField;
import com.baor.enums.FuncEnum;
import com.baor.service.InvoiceService;
import com.baor.utils.AjaxResult;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class InvoiceServiceImpl implements InvoiceService {
    @Autowired
    private InvoiceDao invoiceDao;

    private Page<InvoiceVo> getPage(Map<String,Object> map){
        if ( map.get("pageIndex") == null || "".equals(map.get("pageIndex")) )
            map.put("pageIndex",1);
        if ( map.get("pageSize") == null || "".equals(map.get("pageSize")) )
            map.put("pageSize",10);
        Long pageIndex = Long.valueOf(map.get("pageIndex").toString());
        Long pageSize = Long.valueOf(map.get("pageSize").toString());
        return new Page<>(pageIndex,pageSize);
    }


    private MPJLambdaWrapper<InvoiceVo> getMpj(){
        MPJLambdaWrapper<InvoiceVo> mpj = new MPJLambdaWrapper<>();
        mpj
                .select(InvoiceVo::getInvoiceId)//主键
//                .select(InvoiceVo.class,x -> !x.getColumn().equals("entity_column"))//主表类,不含字段
                .selectAll(InvoiceVo.class)//不需要第二行的话,第一行一同删除,用这行代替
                .selectAs(InvoiceTypeField::getInvoiceTypeName,InvoiceVo::getInvoiceTypeId)//联表字段,(as),主表字段
                .leftJoin(InvoiceTypeField.class,InvoiceTypeField::getInvoiceTypeId,InvoiceVo::getInvoiceTypeId);//联表类,(on),链表联合字段,(=),主表联合字段
        return mpj;
    }


    @Override
    public AjaxResult<?> selectInvoice(Map<String, Object> map) {
//        params.put("pageIndex", (int)params.get("pageSize") * ((int)(params.get("pageIndex")) - 1));
//        params.put("pageSize", params.get("pageSize"));
//        Map<String,Object> res = new HashMap<>();
//        res.put("selectInvoice", invoiceDao.selectInvoice(params));
//        res.put("pageCount", invoiceDao.pageCount(params));
//        return AjaxResult.success(res);
        if ( map.get("siteId") == null || "".equals(map.get("siteId")) )
            return AjaxResult.error("siteId is null");
        MPJLambdaWrapper<InvoiceVo> mpj = getMpj();
        if ( map.get("invoiceName") != null && !"".equals(map.get("invoiceName")) )
            mpj.like(InvoiceVo::getInvoiceName,map.get("invoiceName"));
        if ( map.get("invoiceStatus") != null && !"".equals(map.get("invoiceStatus")) )
            mpj.eq(InvoiceVo::getInvoiceStatus,map.get("invoiceStatus"));
        if ( map.get("createStartTime") != null && ! "".equals(map.get("createStartTime")) )//>
            mpj.gt(InvoiceVo::getCreateTime,map.get("createStartTime"));//ge >=
        if ( map.get("createEndTime") != null && ! "".equals(map.get("createEndTime")) )//>
            mpj.lt(InvoiceVo::getCreateTime,map.get("createEndTime"));//ge >=
        if ( map.get("StartTime") != null && ! "".equals(map.get("StartTime")) )//>
            mpj.gt(InvoiceVo::getStartTime,map.get("StartTime"));//ge >=
        if ( map.get("EndTime") != null && ! "".equals(map.get("EndTime")) )//>
            mpj.lt(InvoiceVo::getStartTime,map.get("EndTime"));//ge >=
        Page<InvoiceVo> page = invoiceDao.selectPage(getPage(map),mpj);
        //-------- 结果处理 --------
        List<InvoiceVo> records = page.getRecords();
        long pages = page.getPages();
        //-------- 数据封装 --------
        Map<String,Object> res = new HashMap<>();
        res.put("pageCount",pages);
        res.put("selectInvoice",records);
        return AjaxResult.success(res);

    }

    @Override
    public AjaxResult<?> selectInvoiceDetail(Map<String, Object> params) {
        return AjaxResult.success(invoiceDao.selectInvoiceDetail(params));
    }

    @Override
    public AjaxResult<?> insertInvoice(Map<String, Object> params) {
        if(this.invoiceDao.insertInvoice(params) > 0){
            return AjaxResult.success("新增发票成功！");
        }else{
            return AjaxResult.error("新增发票失败！");
        }
    }

    @Override
    public AjaxResult<?> selectInvoiceType() {
        return AjaxResult.success(invoiceDao.selectInvoiceType());
    }

    @Override
    public AjaxResult<?> updateInvoiceStatus(Map<String, Object> params) {
       if(this.invoiceDao.updateInvoiceStatus(params) > 0){
            return AjaxResult.success("修改发票状态成功！");
       }else{
            return AjaxResult.error("修改发票状态失败！");
       }
    }
}
